package panels;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JPanel;

public class SPanel extends JPanel {
	private static final long serialVersionUID = -8003927064823402599L;
	public static final int VERTICAL = 0;
	public static final int HORIZONTAL = 1;
	private int padding = 0;
	private int space = 5;
	private int style = SPanel.HORIZONTAL;

	public SPanel() {
		setBackground(Color.WHITE);
		setLayout(null);
	}

	public void setCss(int padding, int[] arr) {
		setPadding(padding);
		setPercent(arr);
	}

	public void setPercent(int[] arr) {
		int hei = getHeight();
		int wid = getWidth();
		int chi = getComponentCount();
		int x = padding;
		int y = padding;
		if (hei <= 0 || wid <= 0) {
			System.out.println("width or height is 0 !!");
			return;
		}
		if (style == SPanel.HORIZONTAL) {
			hei = hei - chi * space - 2 * padding;
			wid = wid - 2 * padding - 2 * space;
		} else {
			wid = wid - chi * space - 2 * padding;
			hei = hei - 2 * padding;
		}
		for (int i = 0; i < chi; i++) {
			int index = i % arr.length;
			if (0 < arr[index]) { 
				Component jc = getComponent(i);
				jc.setLocation(x, y);
				if (style == SPanel.HORIZONTAL) {
					jc.setSize(wid, hei * arr[index] / 100);
					y += hei * arr[index] / 100 + space;
				} else {
					jc.setSize(wid * arr[index] / 100, hei);
					x += wid * arr[index] / 100 + space;
				}
			}
		}
	}

	public void setSpace(int sPACE) {
		space = sPACE;
	}

	public void setPadding(int padding) {
		this.padding = padding;
	}

	public void setStyle(int style) {
		this.style = style;
	}
}
